# -*- coding: utf-8 -*-

from flask.ext.restful import reqparse
import functools
from PalmWebsite.model import Site, SiteSE
from PalmWebsite.extensions import db
import flask

parser = reqparse.RequestParser()
parser.add_argument("domain", type=str)
parser.add_argument("refresh", type=bool)


def request_cache(req):

    @functools.wraps(req)
    def wraps(*args, **kwargs):
        req_args = parser.parse_args()
        domain = req_args.get("domain", None)
        path = flask.request.path[3:]

        if path.startswith("site_index"):
            # site index
            se = kwargs["se"]
            # 查询 此域名下 se_site 的今天的收录信息
            site_index = db.session.query(eval("SiteSE.%s_index" % se)).join(Site).\
                                      filter(SiteSE.site_id==Site.id).\
                                      filter(Site.domain==domain).first()
                                      #filter(SiteSE.last_update_date).first()
            print site_index
            pass
        elif path.startswith("site_link"):
            # site link
            se = kwargs["se"]
            pass


        return {
                "domain": domain
            }

        return req(*args, **kwargs)

    return wraps
